﻿using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace WeaponEvalue.Logic
{
    public class AutoPickItem
    {
        private const string MaoScript = "SetNpcPos 狮鹫之羽 毕埃尔草原 Location\r\nGetTask 是 From 狮鹫之羽";
        private const string QiuScript = "SetNpcPos 雷电之花 阿尔比河 Location\r\nGetTask 是 From 雷电之花";

        private readonly string maoPath = @"D:\KTZ\data\cfg\script\毛.zmy";
        private readonly string qiuPath = @"D:\KTZ\data\cfg\script\球.zmy";

        private readonly GameMonitor gameMonitor;
        private readonly int x;
        private readonly int y;
        private readonly int z;


        public AutoPickItem(GameMonitor gameMonitor, int x, int y, int z)
        {
            this.gameMonitor = gameMonitor;
            this.x = x;
            this.y = y;
            this.z = z;

            if (File.Exists(Application.StartupPath + "\\we.ini"))
            {
                var streamReader = new StreamReader(Application.StartupPath + "\\we.ini");
                maoPath = streamReader.ReadLine();
                qiuPath = streamReader.ReadLine();
                streamReader.Close();
            }
        }

        public void Execute(object param)
        {
            if (gameMonitor.NpcName.StartsWith("雷"))
            {
                var temp = QiuScript.Replace("Location", string.Format("{0} {1} {2}", x, y, z));
                File.WriteAllText(qiuPath, temp, Encoding.Default);
                UIOperator.OperateButton(gameMonitor.F12ButtonPtr);
                UIOperator.OperateDropDown(gameMonitor.ScriptComoboBoxPtr, "球");
                UIOperator.OperateButton(gameMonitor.F12ButtonPtr);
                Thread.Sleep(1000);
                while (!UIOperator.GetText(gameMonitor.F12ButtonPtr).StartsWith("开始"))
                {
                    Thread.Sleep(1000);
                }
                UIOperator.OperateDropDown(gameMonitor.ScriptComoboBoxPtr, 0);
                Thread.Sleep(500);
                UIOperator.OperateButton(gameMonitor.F12ButtonPtr);
                Thread.Sleep(60000);
                UIOperator.OperateButton(gameMonitor.F12ButtonPtr);
                Thread.Sleep(5000);
                UIOperator.OperateDropDown(gameMonitor.PointComoboBoxPtr, "球");
                Thread.Sleep(500);
                UIOperator.OperateButton(gameMonitor.F12ButtonPtr);
            }
            else if (gameMonitor.NpcName.StartsWith("狮"))
            {
                var temp = MaoScript.Replace("Location", string.Format("{0} {1} {2}", x, y, z));
                File.WriteAllText(maoPath, temp, Encoding.Default);
                UIOperator.OperateButton(gameMonitor.F12ButtonPtr);
                UIOperator.OperateDropDown(gameMonitor.ScriptComoboBoxPtr, "毛");
                UIOperator.OperateButton(gameMonitor.F12ButtonPtr);
                Thread.Sleep(1000);
                while (!UIOperator.GetText(gameMonitor.F12ButtonPtr).StartsWith("开始"))
                {
                    Thread.Sleep(1000);
                }
                UIOperator.OperateDropDown(gameMonitor.ScriptComoboBoxPtr, 0);
                Thread.Sleep(500);
                UIOperator.OperateButton(gameMonitor.F12ButtonPtr);
                Thread.Sleep(60000);
                UIOperator.OperateButton(gameMonitor.F12ButtonPtr);
                Thread.Sleep(5000);
                UIOperator.OperateDropDown(gameMonitor.PointComoboBoxPtr, "毛");
                Thread.Sleep(500);
                UIOperator.OperateButton(gameMonitor.F12ButtonPtr);
            }
        }
    }
}
